package com.cnap.service.impl;

import cn.hutool.core.util.IdUtil;
import com.cnap.CnapApplicationTests;
import com.cnap.db.entity.IndicatorCasePO;
import com.cnap.db.entity.IndicatorPO;
import com.cnap.db.entity.MicroServicePO;
import com.cnap.db.mapper.IndicatorMapper;
import com.cnap.db.mapper.MicroServiceMapper;
import com.cnap.model.indicator.IndicatorTypeEnum;
import com.cnap.model.indicator.IndicatorVO;
import com.cnap.model.indicator.QueryIndicatorParam;
import com.cnap.model.indicator.UpdateIndicatorParam;
import com.cnap.service.IndicatorService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Collections;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class IndicatorServiceImplTest extends CnapApplicationTests {

    @Autowired
    private IndicatorService indicatorService;

    @MockBean
    private IndicatorMapper indicatorMapper;

    @MockBean
    private MicroServiceMapper microServiceMapper;

    @Test
    void batchInsertIndicatorCases() {
        Mockito.when(indicatorMapper.batchInsertIndicatorCases(Mockito.anyList())).thenReturn(1);

        IndicatorCasePO indicatorCasePO = new IndicatorCasePO();
        indicatorCasePO.setId(IdUtil.simpleUUID());
        indicatorCasePO.setIndicatorId("1");
        indicatorCasePO.setValue("value");
        indicatorCasePO.setKey("key");
        indicatorCasePO.setUnit("unit");
        indicatorCasePO.setAppId("appId");

        boolean result = indicatorService.batchInsertIndicatorCases(Collections.singletonList(indicatorCasePO));

        Assertions.assertTrue(result);
    }


    @Test
    void getAllIndicatorByServiceId() {
        MicroServicePO microServicePO = new MicroServicePO();
        microServicePO.setApplicationId("appId");
        Mockito.when(microServiceMapper.selectMicroServiceById(Mockito.anyString())).thenReturn(microServicePO);
        IndicatorCasePO calIndicator = new IndicatorCasePO();
        calIndicator.setId(IdUtil.simpleUUID());
        calIndicator.setIndicatorId("1");
        calIndicator.setValue("cal-value");
        calIndicator.setKey("cal-key");
        calIndicator.setUnit("cal-unit");
        calIndicator.setAppId("appId");

        IndicatorCasePO netIndicator = new IndicatorCasePO();
        netIndicator.setId(IdUtil.simpleUUID());
        netIndicator.setIndicatorId("2");
        netIndicator.setValue("net-value");
        netIndicator.setKey("net-key");
        netIndicator.setUnit("net-unit");
        netIndicator.setAppId("appId");

        Mockito.when(indicatorMapper.selectByApplicationIdAndIndicatorType("appId",
                IndicatorTypeEnum.CAL.getValue())).thenReturn(Collections.singletonList(calIndicator));
        Mockito.when(indicatorMapper.selectByApplicationIdAndIndicatorType("appId",
                IndicatorTypeEnum.NET.getValue())).thenReturn(Collections.singletonList(netIndicator));

        IndicatorVO indicatorVO = indicatorService.getAllIndicatorByServiceId("serviceId");

        Assertions.assertEquals("serviceId", indicatorVO.getServiceId());
        Assertions.assertEquals("appId", indicatorVO.getApplicationId());
        Assertions.assertEquals(1, indicatorVO.getCalIndicators().size());
        Assertions.assertEquals(1, indicatorVO.getNetIndicators().size());
    }

    @Test
    void findByPage() {
        QueryIndicatorParam param = new QueryIndicatorParam();
        param.setPageSize(10);
        param.setPageNum(1);

        Mockito.when(indicatorMapper.selectIndicatorByParam(param)).thenReturn(Collections.singletonList(new IndicatorPO()));

        Assertions.assertDoesNotThrow(()->{
            indicatorService.findByPage(param);
        });
    }

    @Test
    void batchUpdateValues() {
        UpdateIndicatorParam param = new UpdateIndicatorParam();
        param.setId("id");
        param.setValue("value");

        Mockito.when(indicatorMapper.updateIndicatorValueById("id", "value")).thenReturn(1);
        Mockito.when(indicatorMapper.updateIndicatorcaseValueById("id", "value")).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
          indicatorService.batchUpdateValues(Collections.singletonList(param));
        });

    }

}